package gov.mogaha.ntis.cmm.mob.view;

import gov.mogaha.ntis.cmm.mob.R;
import gov.mogaha.ntis.cmm.mob.util.DisPlayMetricsManager;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TextAlignView extends LinearLayout {

	private final Context mContext;
	private LayoutInflater inflater;
	private int mTxtLen = 0;
	private boolean mIsPadding;
	
	public TextAlignView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;
		init();
	}

	public TextAlignView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		init();
	}

	private void init(){
		mIsPadding = true;
		int padding = DisPlayMetricsManager.getPixel(5);
		setBackgroundColor(getResources().getColor(R.color.form_label_background));
		setPadding(padding, 0, padding, 0);
		String str = (String)getTag();
		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if(str!=null)
			setTextAlign(str.toCharArray());

	}
	
	private void setTextAlign(char[] charArr){
		
		mTxtLen = charArr.length;
		if(mTxtLen==0) return;

		addView(getTextView(String.valueOf(charArr[0])));

		for(int i = 1; i < charArr.length; i ++){
			
			if(String.valueOf(charArr[i]).equals("("))
				mIsPadding = false;

			if(mIsPadding)
				addView(getTextPaddingView());
			
			addView(getTextView(String.valueOf(charArr[i])));
			
			if(String.valueOf(charArr[i]).equals(")")){
				mIsPadding = true;
			}
			
			
		}

	}
	
	private TextView getTextView(String s){
		
		TextView textView = (TextView)inflater.inflate(R.layout.textalignview_textview, null);
		if(mTxtLen == 1){
			textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		} else{
			textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
		}
		textView.setTypeface(null, Typeface.BOLD);
		textView.setTypeface(Typeface.SERIF);
		textView.setText(s);
		return textView;
	}
	
	private TextView getTextPaddingView(){
		
		TextView mPaddingTextView  = (TextView)inflater.inflate(R.layout.textalignview_padding, null);
		mPaddingTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, 1));
		return mPaddingTextView;
	}
	
	
	//새로운 텍스트 내용 설정 - 이전봉
	public void setText(String str){
		this.removeAllViews();
		setTag(str);
		init();
	}
}






